﻿@rendermode InteractiveServer

<MCard Class="activity-card" Flat Color="transparent">
    <div class="activity-card__header">
        <span class="activity-card__date">@Data.Date.ToString("MM.dd")</span>
        <span class="activity-card__time">@Data.StartTime.ToString("HH:mm") - @Data.EndTime.ToString("HH:mm")</span>
    </div>

    <div class="activity-card__desc" @onclick="GoTo">
        <MImage Src="@Data.Cover" 
                Contain
                Eager>
        </MImage>
    </div>

    <div class="activity-card__footer">
        @{
            var logo = GetProductLogo(Data.Product);

            if (logo is not null)
            {
                <MAvatar Size="36" Color="#D9D1FF" Class="activity-card__group">
                    <MImage Src="@logo" Width="20" Height="20" Contain Eager Style="border-radius: 0;"></MImage>
                </MAvatar>
            }
        }
        <PBlockText PrimaryClass="activity-card__title" SecondaryClass="activity-card__subtitle">
            <PrimaryContent>
                <div @onclick="GoTo">@Data.Title</div>
                @{
                    var value = FormatActivityMode(Data.Mode);
                    if (value.HasValue)
                    {
                        <MChip Dark Label Small Color="@value.Value.color" Class="ml-2">@value.Value.text</MChip>
                    }
                }
            </PrimaryContent>
            <SecondaryContent>
                @Data.Subtitle
            </SecondaryContent>
        </PBlockText>
    </div>
</MCard>

@code {

    [Inject]
    public NavigationManager NavigationManager { get; set; } = null!;

    [Parameter, EditorRequired]
    public Activity Data { get; set; } = null!;

    private static Dictionary<ActivityProduct, string?> s_productMap = new()
    {
        { ActivityProduct.Stack, "https://cdn.masastack.com/stack/images/logo/MASAStack/logo.png?x-oss-process=image/resize,l_20" },
        { ActivityProduct.Framework, "https://cdn.masastack.com/images/framework_logo.png?x-oss-process=image/resize,l_20" },
        { ActivityProduct.Blazor, "https://cdn.masastack.com/stack/images/website/masa-blazor/miniLogo.png?x-oss-process=image/resize,l_20" }
    };

    private(string text, string color)? FormatActivityMode(ActivityMode mode)
    {
        return mode switch
        {
            ActivityMode.Online  => ("直播", "green"),
            ActivityMode.Offline => ("线下", "blue"),
            _                    => null
        };
    }

    private string? GetProductLogo(ActivityProduct product)
    {
        return s_productMap.GetValueOrDefault(product);
    }

    private void GoTo()
    {
        if (Data.Id == null)
        {
            return;
        }

        NavigationManager.NavigateTo($"/activityDetail/{Data.Id}", false);
    }

}
